<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String path = request.getContextPath(); 
String strUserId = request.getSession().getAttribute("stxtUName").toString();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
  <head>
  	<style type="text/css" media="all">   
	    .allow-float {clear:none!important;} /* 允许该元素浮动 */  
	    .stop-float {clear:both!important;} /* 阻止该元素浮动 */  
	   	.icon-first {float:left;}   
	   	.icon-choosed {float:left;padding:5 0 0 5px;}
	    .icon-second {float:left;padding:0 0 0 20px;} 
	    .win-first {float:left;}   
	    .win-second {float:left;padding:0 0 0 20px;}   
	   	.standby-yes {float:left;}   
	    .standby-no {float:left;padding:0 0 0 20px;}   
	    .age-field {float:left;padding:0 0 0 58px;*padding:0 0 0 50px!important;*padding:0 0 0 50px;}   
	</style> 
  
    <base href="<%=basePath%>"> 
    <link rel="stylesheet" type="text/css" href="<%=path%>/js/ext/resources/css/ext-all.css">  
	<link rel="stylesheet" type="text/css" href="<%=path%>/js/ext/resources/css/edit-grid.css">  
	<script type="text/javascript" src="<%=path%>/js/ext/adapter/ext/ext-base.js"></script>
	<script type="text/javascript" src="<%=path%>/js/ext/ext-all.js"></script>
	<script type="text/javascript" src="<%=path%>/js/ext/DWRTreeLoader.js"></script> 
	<script type="text/javascript" src="<%=path%>/js/ext/query-dwr-proxy.js"></script>	
	<script type="text/javascript" src="<%=path%>/js/ext/ext-lang-zh_CN.js"></script>
	<script type='text/javascript' src='/btsm/dwr/interface/JSystemWeb.js'></script>
	<script type='text/javascript' src='/btsm/dwr/engine.js'></script>
	<script type='text/javascript' src='/btsm/dwr/util.js'></script>	 
  </head>
<body> 	
	<div id="menuTree" style="height:80%"/>
	<div id="menuContent"/>  
	<div id="parenTreeDiv"/> 
	<input type= "hidden " name= "oldParentMenu "  id="oldParentMenuID" style="display:none"/> 
<script type="text/javascript">

var isAdd = true;

Ext.onReady(function(){
	var viewport = new Ext.Viewport({
		layout:'border',
		frame:true,
		items:[
			{
				region: 'west',
				contentEl: 'menuTree',
				margins:'110 5 30 5',
				split: true,
				border: true,
				collapsible: true,
				width: 300
			},
			{
				region: 'center',
				contentEl: 'menuContent',
				margins:'110 30 30 5',
				split: true,
				border: true	
			}]
		});
		
	var menuTreeDiv = Ext.get("menuTree");	
	var Tree = Ext.tree;    
    var menuRoot = new Tree.AsyncTreeNode({
           text: '顶部菜单', 
		   icon:'/btsm/js/ext/resources/images/user.gif',
           draggable:false, 
           id:'0'
     });	 
	var treePanel = new Ext.tree.TreePanel( {
		el : "menuTree",
		border : false,
		animate : true,
		autoHeight : true,
		height:menuTreeDiv.getComputedHeight(),
		rootVisible : true,
		autoScroll : true,
		containerScroll : true,
		loader : new Ext.tree.DWRTreeLoader( {
			dwrCall:JSystemWeb.findMenuTree,queryParam:{id:0,strUserID:'<%=strUserId%>',strDepth:1}
		}),
		root : menuRoot,
		tbar: new Ext.Toolbar( [
					{xtype:"tbfill"},
					{xtype:"tbseparator"},
					{
						text: '新建',
						pressed:true,
						handler:function()
						{	    	     	    
							menuForm.getForm().reset();	
							isAdd = true;
						}
					},
					{xtype:"tbseparator"},
					{
						text: '删除',
						pressed:true,
						iconCls:'remove',
						handler:function()
						{	
							var seletedNode = treePanel.getSelectionModel().getSelectedNode();
							if(seletedNode == null)
							{
								Ext.Msg.alert("提示","请选择要删除的菜单，然后再点击【删除】按钮 !");
								return false;
							}
							var LEAF_FLAG = Ext.get('LEAF_FLAG').dom.value;
							var id = seletedNode.id;
							
							if(LEAF_FLAG == "false")//不是叶子
							{
								Ext.Msg.alert("提示","只能删除叶子节点，该菜单有子菜单，请重新选择需要删除的菜单 !");
								return false;
							}
														
							var parentID = Ext.get('PARENT_ID').dom.value;
							//alert("id = "+id+" parentID = "+parentID);
														
							Ext.MessageBox.confirm('确认删除', "确认删除菜单【"+seletedNode.text+"】?", 
							function(btn) 
							{
								 if(btn == 'yes') 
								 {// 选中了是按钮
									
									JSystemWeb.deleteMenyByID(id,parentID,function(data){
									if(data)
									{
										Ext.Msg.alert('成功', '删除菜单成功 ！',function(){ 
												menuRoot.reload();
												menuForm.getForm().reset();	
											});
									}else
										Ext.Msg.alert('失败', '删除菜单失败 ！');
									});	
									
								 }
							});
						}
					}
				])
	});
	treePanel.render();
	//menuRoot.expand(true,true);	

	//菜单树点击事件
	treePanel.on("click",function(node,event)
	{
		if(node.id == 0)
			return;
		isAdd = false;
		JSystemWeb.getMenuInfoByID(node.id,function(obj)
		{
			if(obj != null)
			{
				var data = {
							'id':obj.ID,
							'NAME':obj.NAME,
							'PARENT_ID':obj.PARENT_ID,
							'ORDER_':obj.ORDER_,
							'LINK':obj.LINK,
							'TIP':obj.TIP,
							'parentMenuName':node.parentNode.text,
							'LEAF_FLAG':obj.LEAF_FLAG
						   };
				var record = new Ext.data.Record(data);	 
				Ext.get('oldParentMenuID').dom.value   = obj.PARENT_ID;	     	    
				menuForm.getForm().loadRecord(record);			   
			}
		})
	})
	
	
	//选择父节点图标
	var chooseParentButton = new Ext.Button({
					pressed : true,
					enableToggle : true,
					text : '选择父节点',
					toggleHandler:doChoose
		});	

	var menuForm = new Ext.FormPanel({
        labelWidth: 150,
        frame:true,
        title: '菜单设置',   
        defaultType: 'textfield',
		buttonAlign:'right',
        items: [
				{
					xtype : 'hidden',
					name: 'id'
				},{
					fieldLabel: '名称',
					name: 'NAME',
					allowBlank:false,
					width:200,
					blankText :'请输入菜单名称'	
				},{
					fieldLabel: '描述',
					name: 'TIP',
					allowBlank:false,
					width:200,
					blankText :'请输入菜单描述'	
				},{
					xtype:'numberfield',
					fieldLabel: '排序',
					name: 'ORDER_',
					allowBlank:false,
					width:200,
					blankText :'请输入菜单序号'	
				},{
					fieldLabel: '链接',
					width:200,
					name: 'LINK'
				},
				{
	            	xtype : 'hidden',
	                fieldLabel: '上层菜单选择',
	                name: 'PARENT_ID',
	                anchor:'60%'	                
	            },{
	                fieldLabel: '上层菜单选择',
	                name: 'parentMenuName',
	                readOnly : true,
	                itemCls:'win-first', //向左边浮动,处理控件横排   
			        clearCls:'allow-float', //允许两边浮动,在实际生成的HTML结构中有专门的DIV阻断浮动  
	                width:200	                
	            },chooseParentButton,
	            {
		        	xtype : 'hidden',
					fieldLabel : "LEAF_FLAG",
					readOnly : true,
					name : "LEAF_FLAG"
	        	}
        ],
        buttons: [
				{
					text: '后退',
					handler:function()
					{
						history.go(-1);
					}
				},{
					text: '保存',
					handler:saveMenuInfo
				},{
					text: '取消',
					handler:function()
					{
						isSelected = false ;
						menuForm.getForm().reset(); 
					}
				}]
    });

    menuForm.render("menuContent");
	var parentPanel = new Ext.FormPanel({
        labelWidth: 75,
        frame:true,
        //title: '总体信息',  
		height:600, 
        defaultType: 'textfield',
        items: menuForm
		});
	parentPanel.render("menuContent");	
	
	
	
	function saveMenuInfo()
	{
		if (!menuForm.getForm().isValid())
			return;	
		var form = menuForm.getForm().getValues(false);
		SysmMenuBO = {};
		SysmMenuBO.ID = form.id;
		SysmMenuBO.NAME = form.NAME; 
		SysmMenuBO.ORDER_ = form.ORDER_;
		SysmMenuBO.LINK = form.LINK;
		SysmMenuBO.PARENT_ID = form.PARENT_ID;
		SysmMenuBO.TIP = form.TIP;
		SysmMenuBO.LEAF_FLAG = form.LEAF_FLAG;
		SysmMenuBO.MENU_TYPE = 0;
		JSystemWeb.createMenu(SysmMenuBO,Ext.get('oldParentMenuID').dom.value,function(data)
		{
			if(data)
			{ 
				Ext.Msg.alert('成功',"操作成功！"); 
				menuForm.getForm().reset();
				menuRoot.reload();
				//menuRoot.expand(true,true);	
				parentRoot.reload();
			}else
			{
				Ext.Msg.alert("失败","操作失败!");
			}	
		})
	
	}
	
	function doChoose()
	{
		parentTree.render();
		parentRoot.expand();
		win.show();	
	}
	
	//父节点树
	var parentTree = new Ext.tree.TreePanel({
		el:'parenTreeDiv',
		animate:true, 
		checkModel:'single',
		loader : new Ext.tree.DWRTreeLoader( {
				dwrCall:JSystemWeb.getParentMenuTree,queryParam:{id:0,strEditMenuId:0}
			}),
		enableDD:true,
		containerScroll: true,
		parentTree:menuTreeDiv.getComputedHeight(),
		border:false,
		lines:false,
		rootVisible:false,
		autoScroll:true,
		dropConfig: {appendOnly:true}
	});
					
	parentRoot = new Ext.tree.AsyncTreeNode({
		text: '系统菜单', 
		draggable:false, // disable root node dragging
		id:'0'
	});
	parentTree.setRootNode(parentRoot);
	
	//选择父节点弹出窗口
	var win = new Ext.Window({
					//el:'hello-win',
					layout:'fit',
					width:300,
					height:400,
					modal : true,
					closeAction:'hide',
					plain: true,
					items:parentTree,		
					buttons: [
						{
							text:'确&nbsp;&nbsp;定',
							handler: function()
							{
								var selectedNode = parentTree.getSelectionModel().getSelectedNode();
								if(selectedNode == null)
								{
									Ext.Msg.alert("提示","请先选择父菜单，然后再点击【确定】按钮 !");
									return false;
								}
								if(!isAdd)	
								{
									if(treePanel.getSelectionModel().getSelectedNode().id == selectedNode.id)
									{
										Ext.Msg.alert("提示","不能选择自己作为上层菜单 !");
										return false;
									}
								}
								
								var id = selectedNode.id;
								Ext.get('PARENT_ID').dom.value = id;
								Ext.get('parentMenuName').dom.value = selectedNode.text;
								win.hide();
							}
						},
						{
							text: '取&nbsp;&nbsp;&nbsp;消',
							handler: function()
							{
								win.hide();
							}
						}]
				});
	
	
})

</script>
	
</body>
</html>